﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DTO;
using BUS;

namespace Support_Study_Vocabulary
{
    public partial class frmEditTopic : Office2007Form
    {
        private bool _isOk;
        private string _word;
        private string _type;
        private TOPIC _oldTopic;
        private TOPIC _newTopic;
        
        public bool IsOk
        {
            get { return _isOk; }
            set { _isOk = value; }
        }
        
        public string Word
        {
            get { return _word; }
            set { _word = value; }
        }        

        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public TOPIC OldTopic
        {
            get { return _oldTopic; }
            set { _oldTopic = value; }
        }

        public TOPIC NewTopic
        {
            get { return _newTopic; }
            set { _newTopic = value; }
        }

        public frmEditTopic()
        {
            InitializeComponent();
            _isOk = false;
            _newTopic = new TOPIC();
        }

        private void chbNewTopic_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chbNewTopic.Checked == true)
            {
                this.cbNewTopic.Enabled = false;
                this.txtNewTopic.Enabled = true;
            }
            else
            {
                this.cbNewTopic.Enabled = true;
                this.txtNewTopic.Enabled = false;
            }
        }

        private void frmEditTopic_Load(object sender, EventArgs e)
        {
            this.txtOldTopic.Text = _oldTopic.Name;            
            List<TOPIC> lsTopic = TopicBUS.getAllTopics();
            this.cbNewTopic.DisplayMember = "Name";
            this.cbNewTopic.ValueMember = "idTopic";
            this.cbNewTopic.DataSource = lsTopic;
            this.cbNewTopic.SelectedValue = _oldTopic.idTopic;
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Close_Click(object sender, EventArgs e)
        {            
            _newTopic.Name = this.cbNewTopic.Text;
            if (this.chbNewTopic.Checked == true)
            {             
                _newTopic.Name = this.txtNewTopic.Text;
                if (TopicBUS.getTopic(txtNewTopic.Text) != null)
                {
                    MessageBox.Show("Chủ đề này đã có rồi.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.txtNewTopic.Focus();
                    _isOk = false;
                    return;
                }

                if (TopicBUS.Add(_newTopic) == false)
                {
                    MessageBox.Show("Tạo topic thất bại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    _isOk = false;
                    return;
                }                
            }

            _newTopic = TopicBUS.getTopic(_newTopic.Name);

            if (VocabularyBUS.getVocabulary(_word, _type, _newTopic.idTopic) != null)
            {
                string report = "\"[word] ([type])\" thuộc chủ đề \"[topic]\" đã tồn tại.";
                report = report.Replace("[word]", _word);
                report = report.Replace("[type]", _type);
                report = report.Replace("[topic]", _newTopic.Name);                
                MessageBox.Show(report, "Thông báo");
                return;
            }

            //VOCABULARY vcNew = VocabularyBUS.getVocabulary(_word,_type,_oldTopic.idTopic);
            //vcNew.idTopic = _newTopic.idTopic;
            //VocabularyBUS.Edit(_word, _type, _oldTopic.idTopic, vcNew);
            _isOk = true;
            this.Close();
        }
    }
}
